<?php
class class_timkiem
{
	//Khai bao bien
	var $arr_timkiem;//luu tat ca gia tri tim kiem vao array
	var $arr_cat_all;
	var $arr_catcontent_all;
	
	function class_timkiem()	
	{
		global $arr_menu_id, $langid;
		//Khoi tao bien
		//$bien = 0;
		$arr_timkiem = array();
		$arr_cat_all = array();
		$arr_catcontent_all = array();

	}
	//thay the gia tri nhu submit file, button...
	function loadtemplate($arr)
	{
		$html = "";
		$html = loadHtml($arr["layout_html"],"");
		if (count($arr) > 0){
			foreach ($arr as $key=>$value){
				//$$key = $value;	//gan gia tri vao bien
				$pattern = "@$key@";
				$replacement = "$value";
				$html = ereg_replace($pattern, $replacement, $html);
			}//end for
		}
		return $html;	
	}

	function array_noidung($q)
	{
		global $dbconn, $arr_menu_id, $langid;
		$link = "";
			
		//print_r ($this->arr_cat_all); echo"<br>";
		$catcontent_list = arr_to_string($this->arr_catcontent_all);
		//echo "$catcontent_list<br>";
		/*
		$sql = "SELECT news_id as id, news_title as tieude, news_cat  ";
		$sql .= "from content where news_cat in (".$catcontent_list.")";
		$sql .= " and MATCH (news_title,news_description,news_fulltext) AGAINST ('".$q."')";
		*/
		$sql = "SELECT news_id as id, news_title as tieude, news_cat  ";
		$sql .= "from content where news_cat in (".$catcontent_list.")";		
		$sql .= " and (";
		$sql .= "news_title like '%".$q."%' or ";
		$sql .= "news_description like '%".$q."%' or ";
		$sql .= "news_fulltext like '%".$q."%' ";		
		$sql .= ")";
		//echo "$sql<br>";
		$rs = new db_query($dbconn,$sql);
		$arr_menu_id = array();

		while ($rs->getrow()){
			//echo "$i - ";
			$id = $rs->row["id"];
			$arr_menu_id = get_menu_id($id);
			
			$tieude = $rs->row["tieude"];
			if (trim($tieude) == ""){
				$tieude = "......";
			}
			//$this->arr_timkiem["tieude"][] = $tieude;

			//$link = "<a href=\"javascript:OpenWindow('popup.php?popupid=".$id."&langid=".$langid."','','width=600,height=400,toolbar=no,location=no,status=no,menubar=no,resizable=yes,scrollbars=yes,alwaysRaised,dependent,titlebar=no')\" class=\"button_link\" >".$tieude."</a>";
			$url = "detail.php?catid=".$arr_menu_id["catid"]."&subcatid=".$arr_menu_id["subcatid"]."&contentid=".$id."&langid=".$langid;
			$link = "<a href=\"".$url."\" class=\"button_link\" >".$tieude."</a>";
			$this->arr_timkiem[] = $link;

		}
	}
		
	function array_sanpham($q)
	{
		global $dbconn, $arr_menu_id, $langid;
		$link = "";
			
		//print_r ($this->arr_cat_all); echo"<br>";
		$cat_list = arr_to_string($this->arr_cat_all);
		//echo "$cat_list<br>";
		
		$sql = "SELECT proid as id, catid, name as tieude ";
		$sql .= "from sanpham where catid in (".$cat_list.")";
		$sql .= " and (";
		$sql .= "name like '%".$q."%' or ";
		$sql .= "fd01 like '%".$q."%' or ";
		$sql .= "fd02 like '%".$q."%' or ";		
		$sql .= "fd04 like '%".$q."%' ";		
		$sql .= ")";		
//		$sql .= " and MATCH (name,fd01,fd02,fd04) AGAINST ('".$q."')";
		//echo "$sql<br>";
		$rs = new db_query($dbconn,$sql);
		while ($rs->getrow()){
			$id = $rs->row["id"];
			$tieude = $rs->row["tieude"];
			//$this->arr_timkiem["tieude"][] = $tieude;

			$link = "<a href=\"javascript:OpenWindow('popup.php?popupid=".$id."&langid=".$langid."','','width=600,height=400,toolbar=no,location=no,status=no,menubar=no,resizable=yes,scrollbars=yes,alwaysRaised,dependent,titlebar=no')\" class=\"button_link\" >".$tieude."</a>";

			$this->arr_timkiem[] = $link;
		}
	}
	function timkiem_all($q)
	{
		$this->array_noidung($q);
		$this->array_sanpham($q);
	}
	
	function lietke($arr, $arr_column){
		global $which_page, $page_block, $dbconn, $arr_list_page, $langid, $catid, $subcatid, $arr_list_ketqua, $arr_thoigiantim;
		$page_size = 30;
		//load var
		$html = $this->loadtemplate($arr);
		//load templates
		$main_list = loadHtml($arr["tmp_list"],"");		
		$html = ereg_replace("@main_list@", $main_list, $html);
		
		$totalrows = count($this->arr_timkiem);
		if ( $totalrows > 0){		
			$html .= '
					<table width="100%" cellpadding="0" cellspacing="0">
					<tr>
						<td width="50%" height="50" align="left" class="listitem" >@TOTAL@</td>		
						<td width="50%" align="right" class="listitem">@PAGE@</td>
					</tr>
					</table>			
			';
			$tmp_thoigian = "
						<table width='100%' border='0' cellspacing='0' cellpadding='0'>
						  <tr>
							<td align='right'><i>".$arr_thoigiantim[$langid]."</i> </td>
						  </tr>
						</table>			
			";
			$html = $tmp_thoigian.$html;
	
			//Get Paging
			if(!$which_page || !is_numeric($which_page)){
				$which_page = 1;
			}
			$num_page = (int)($totalrows/$page_size);
			if($totalrows%$page_size != 0){
				$num_page ++;
			}
			//$num_row = $page_size;
			if($which_page > $num_page) $which_page = $num_page;
			if($which_page == 1){
				$index = 0;
			}else{
				$index = ($which_page*$page_size) - $page_size;
			}
		
			$body = "";
			$tmp_content = loadHtml($arr["tmp_list_content"], "");
			$i=0;
			$rs= new db_query($dbconn,$strSql);
			foreach ($this->arr_timkiem as $key=>$value)
			{ //begin While
				if (($i >= $index) && ($i < $page_size + $index))
				{
					$stt = $i+1;
					$body .= $tmp_content;
					$id = $rs->row["id"];
					$body = ereg_replace("@id@","$id",$body);
					//echo "key: $key - value: $value<br>";
					foreach ($arr_column as $giatri){
						$value_pattern = "@$giatri@";
						
						switch ($giatri) {
							case "catid":
								$sql = "select cat_name from cat where catid='".$rs->row["$value"]."'";
								$value_replacement = get_values($sql);
								break;
							default:
								$value_replacement = $value;
						}//end switch

						$body = ereg_replace("$value_pattern", "$value_replacement",$body);
					}//end for					
				}
				 $i++;
			}//end While
			$StringQuery = "&catid=$catid&subcatid=$subcatid&langid=$langid\"";
			$str_paging = "";
			if($num_page>1){
				if($which_page > 1 ){
					$str_paging = "<a href=\"?which_page=1$StringQuery> [First] </a>";
					$str_paging .= "&nbsp;<a href=\"?which_page=".($which_page-1)."$StringQuery  >[Previous]</a>&nbsp;";
				}else{
					$str_paging = "";
				}
			
			//=======================================================
				//Duc Manh them vao		
				$str_paging .= list_page($which_page,$num_page,$page_block,$StringQuery);
			//========================================================
			
				if($which_page < $num_page ){
					$str_paging .= "&nbsp;<a href=\"?which_page=".($which_page+1)."$StringQuery  > [Next]  </a>";
					$str_paging .= "<a href=\"?which_page=".($num_page)."$StringQuery> [Last]  </a>";
				}else{
					$str_paging .= "";
				}
			}//end if $num_page > 1
		}else{
			$html .= $arr_list_ketqua[$langid];
		}//end if rs_if

		//===============
		if ($i > 1 && $langid > 0){
			$record_num = $arr_list_page[$langid][0].": ".$i."&nbsp;".$arr_list_page[$langid][1]."s";
		}else{
			$record_num = $arr_list_page[$langid][0].": ".$i."&nbsp;".$arr_list_page[$langid][1];
		}
		$record_num .= " - " . $arr_list_page[$langid][2] . " $which_page/$num_page";

		$html = ereg_replace("<!--list_content-->", $body, $html);
		$html = ereg_replace("@PAGE@", "$str_paging", $html);
		$html = ereg_replace("@TOTAL@", "$record_num", $html);
		$html = ereg_replace("@which_page@", "$which_page", $html);				
		$StringQuery = ereg_replace("\"","",$StringQuery);
		$html = ereg_replace("@StringQuery@", "$StringQuery", $html);
		
		return $html;
	} //end lietke
	

}//end class_timkiem
?>